package com.levi.juc.coll_unsafe.list_set;

import java.util.*;

/**
 * 第一种解决方案  用Collections.sync里面的
 * 第二种方案用juc里面的CopyOnWriteArrayList（推荐）
 * 不推荐用Vector，方法都加了锁，效率太低
 */
public class VectorTest {
    public static void main(String[] args) {
        /**
         * Vector
         */
        Vector<String> vector = new Vector<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                vector.add(UUID.randomUUID().toString().substring(1, 5));
                System.out.println(vector);
            },String.valueOf(i)).start();
        }
        long end = System.currentTimeMillis();
        long use = end - start;
        System.out.println(use);


        /**
         * synchronizedList
         */
        List<String> list = Collections.synchronizedList(new ArrayList<String>());
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(list);
            }).start();
        }
    }


}